﻿/*
	PlayerController - Pretty much sets up player with all relevant components(not completely),
	takes movement input and stores it into a vector to send to the PlayerMotor,
	takes action input and stores the information as well as triggers any events such as animations and such(not yet).
*/

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public static PlayerController instance;
	public static CharacterController characterController;

	public int coins = 0;

	void Awake(){
		instance = this;
		characterController = GetComponent("CharacterController") as CharacterController;
		
		CameraController.Setup();
	}

	void Update () {
		if(Camera.main == null){
			return;
		}

		HandleMotionInput();
		HandleActionInput();

		PlayerMotor.instance.UpdateMotor();
	}

	void HandleMotionInput(){
		float deadZone = 0.1f;

		PlayerMotor.instance.verticalVelocity = PlayerMotor.instance.moveVector.y;
		PlayerMotor.instance.moveVector = Vector3.zero;

		Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

		if(Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone ||
		   Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone){

			if(Input.GetAxis("Horizontal") > 0)
				gameObject.transform.FindChild("PlayerSprite").rotation = Quaternion.Euler(0, 0, 0);
			if(Input.GetAxis("Horizontal") < 0)
				gameObject.transform.FindChild("PlayerSprite").rotation = Quaternion.Euler(0, 180, 0);
			PlayerMotor.instance.moveVector += inputVector;
		}
	}

	void HandleActionInput(){
		if(Input.GetButton("Jump")){
			Jump();
		}
	}

	void Jump(){
		PlayerMotor.instance.Jump();
	}
}
